/*
 * FacesPortletUtil.java
 *
 * Created on 2 de junio de 2006, 9:35
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package reclamoscarguearchivowebapp;

import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.portlet.PortalContext;
import javax.portlet.PortletContext;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.PortletSession;

/**
 *
 * @author root
 */
public class FacesPortletUtil 
{
    public static void bindJNDIAttributeToCurrentUser(FacesContext faces, String attributeName, Object value) 
    {
        try 
        {
            Context ctx = new InitialContext();
            String user = faces.getCurrentInstance().getExternalContext().getRemoteUser();
            ctx.rebind("RadarUserSession." + user + "." + attributeName, value);
        }
        catch(NamingException e) 
        {
            System.out.println("FacesPortletUtil: bindJNDIAttributeToCurrentUser: " +  e.toString() );
            e.printStackTrace();
        }
        
    }
    
    public static Object retrieveJNDIAttributeFromCurrentUser(FacesContext faces, String attributeName)
    {
        try 
        {
            Context ctx = new InitialContext();
            String user = faces.getCurrentInstance().getExternalContext().getRemoteUser();
            return ctx.lookup("RadarUserSession." + user + "." + attributeName);
        }
        catch(NamingException e) 
        {
            System.out.println("FacesPortletUtil: retrieveJNDIAttributeFromCurrentUser: " +  e.toString() );
            e.printStackTrace();
            return null;
        }
    }
    
    public static void unbindJNDIAttributeFromCurrentUser(FacesContext faces, String attributeName) 
    {
        try 
        {
            Context ctx = new InitialContext();
            String user = faces.getCurrentInstance().getExternalContext().getRemoteUser();
            ctx.unbind("RadarUserSession." + user + "." + attributeName);
        }
        catch(NamingException e) 
        {
            System.out.println("FacesPortletUtil: unbindJNDIAttributeFromCurrentUser: " +  e.toString() );
            e.printStackTrace();
        }        
    }
}
